<template>
    <div class="main">
        <login
            v-if="type == 'login'"
            @changePage="fnChangePage"
            @govip="fnGoVip()"
            @success="fnSuccess()"
            @close="fnGoPage('/index')"
        />
        <reg
            v-else-if="type == 'reg'"
            @changePage="fnChangePage"
            @govip="fnGoVip()"
            @success="fnSuccess()"
            @close="fnGoPage('/index')"
        />
    </div>
</template>

<script>
import { mapState } from 'vuex'
import Qs from 'qs'
import login from '@/components/login/login'
import reg from '@/components/login/reg'
import { checkProduct } from '@/assets/js/utils'
export default {
    components: { login, reg },
    data() {
        return {
            type: 'login'
        }
    },
    computed: {
        ...mapState({
            dist: (state) => state.isLogin.dist
        })
    },
    created() {
        if (!process.server) {
            this.$store.commit('header/setStyle', 'default')
            if (this.$route.query.page) {
                this.type = this.$route.query.page
            }
            if (this.$route.query.dist) {
                this.$store.commit('isLogin/SET_DIST', this.$route.query.dist)
            }
        }
    },
    methods: {
        fnGoPage(path) {
            this.$router.push({
                path
            })
        },
        addUrlParamReplace(url, paramName, replaceWith) {
            let nUrl = ''
            const ticket = this.$getSessionTicket()

            if (url.includes('ticket=')) {
                // eslint-disable-next-line no-eval
                const re = eval('/(' + paramName + '=)([^&]*)/gi')

                nUrl = url.replace(re, paramName + '=' + replaceWith)
            } else if (url.includes('?')) {
                nUrl = url + ('&ticket=' + ticket)
            } else {
                nUrl = url + ('?ticket=' + ticket)
            }
            return nUrl
        },
        fnGoVip() {
            this.$store.commit('isLogin/HIDE_LOGIN')
            this.$router.push('/home')
        },
        fnChangePage(type) {
            this.type = type
        },
        fnSuccess() {
            const isProduct = checkProduct()
            let dist = this.dist ? unescape(this.dist) : ''
            // console.log('loginDist', dist)
            const ticket = this.$getSessionTicket()

            if (dist) {
                const urlArr = dist.split('?')
                const data = Qs.parse(urlArr.length == 2 ? urlArr[1] : '')

                // console.log(urlArr, data)

                data.session_ticket = ticket
                dist = urlArr[0] + '?' + Qs.stringify(data)

                location.href = dist
            } else {
                // TODO 修改跳转地址
                location.href = isProduct
                    ? '//cms.zsdx.cn?session_ticket=' + ticket
                    : '//gt.wxhand.com?session_ticket=' + ticket
            }
        }
    }
}
</script>

<style lang="less" scoped>
.main {
    top: 150px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, 0);
}
.close-btn {
    position: absolute;
    width: 26px;
    height: 26px;
    right: -13px;
    top: -13px;
    font-size: 26px;
    color: rgba(255, 255, 255, 0.8);
}
</style>
